package com.atguigu.bigdata.chapter11.sql;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink03_SQL_Kafka_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 通过ddl方式建表, 直接和文件关联, 将来从这个表读取数据, 就自动从文件读取  (官方推荐使用这种)
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts int, " +
                            " vc int" +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv'" +
                            ")");
        
        
        //
        
        tEnv.executeSql("create table sensor_out(" +
                            " id string, " +
                            " a int, " +
                            " b int" +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's2', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'format' = 'json'" +
                            ")");
        //Table result = tEnv.sqlQuery("select * from sensor");
        //        result.executeInsert("sensor_out");
        //tEnv.executeSql("insert into sensor_out select * from sensor");
        // a, 10, 1000
        Table result = tEnv.sqlQuery("select id, vc, ts from sensor");
        // 写入的时候, result是如何和表进行字段对应?  靠的顺序, 不看字段名, 只看类型是否对应
//        result.executeInsert("sensor_out");
        // c,2000,20
        tEnv.executeSql("insert into sensor_out select * from sensor"); // 也是看顺序, 不看字段名, 只看类型是否对应
    }
}
/*
读写文件
    ddl方式
    执行一个建表语句直接和文件进行关联


读写kakfa

 */